﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Web;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Security;
using System.Web.Script.Serialization;
using System.Linq;
using HolcimProject.Function;
using HolcimProject.Common.DTO;

using HolcimProject.Common;

using Microsoft.SharePoint.Client.Services;
namespace HolcimProject.WCFService
{
    [BasicHttpBindingServiceMetadataExchangeEndpoint]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class WCFService : IWCFService
    {

        //IMPORTANT
        //When a web application is configured to use claims authentication (Windows claims, form-based authentication claims, or SAML claims), 
        //the IIS website is always configured to have anonymous access turned on. Your custom SOAP and WCF endpoints may receive requests 
        //from anonymous users. If you have code in your WCF service that calls the RunWithElevatedPrivileges method to access information 
        //without first checking whether the call is from an authorized user or an anonymous user, you risk returning protected SharePoint 
        //data to any anonymous user for some of your functions that use that approach.

        // To test this service, use the Visual Studio WCF Test client
        // set the endpoint to http://<Your server name>/_vti_bin/WCFService.svc/mex

        public string HelloWorld()
        {
            return "Hello World from WCF and SharePoint 2010";
        }

        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        public DataList GetFinding()
        {

            try
            {
                var rs = new DataList
                {

                    aaData = new List<string[]>()
                };
                FunctionFinding functionFinding = new FunctionFinding();
                List<FindingDTO> lst = new List<FindingDTO>();
                lst = functionFinding.GetFinding();
                List<string[]> aaData = new List<string[]>();
                foreach (FindingDTO finding in lst)
                {
                    string linkEdit = "<a href='#' onclick=\"OpenDialogEditTask(" + finding.Id + "," + finding.IdFindingTask + ",'" + finding.StateFindingTask + "')\">Edit</a>";
                    aaData.Add(new string[15]{    
                                                 linkEdit,
                                                   finding.Clause==""?"":finding.Clause.Split('#')[1], 
                                                  finding.AuditeeDepartment==""?"":finding.AuditeeDepartment.Split('#')[1], 
                                                  finding.System==""?"":finding.System.Split('#')[1], 
                                                  finding.Classification==""?"":finding.Classification.Split('#')[1], 
                                                  finding.Auditor==""?"":finding.Auditor.Split('#')[1],
                                                  finding.LeadAuditor==""?"":finding.LeadAuditor.Split('#')[1],                                                  
                                                  finding.Auditee==""?"":finding.Auditee.Split('#')[1],
                                                  finding.PersonInChage==""?"":finding.PersonInChage.Split('#')[1],
                                                  finding.Finding,
                                                  finding.Correction,  
                                                  finding.RootCause, 
                                                  finding.Corrective,                                                  
                                                  finding.LstCompleteDate,
                                                  finding.StateFindingTask
                                                });

                }
                rs.aaData = aaData;
                return rs;
            }
            catch
            {
                return null;
            }
        }

        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        public DataList GetFindingTaskCurrentState()
        {

            try
            {
                var rs = new DataList
                {
                    aaData = new List<string[]>()
                };
                FunctionFinding functionFinding = new FunctionFinding();
                List<FindingDTO> lst = new List<FindingDTO>();
                lst = functionFinding.GetFindingTaskCurrentState();
                List<string[]> aaData = new List<string[]>();
                foreach (FindingDTO finding in lst)
                {
                    string linkEdit = "<a href='#' onclick=\"OpenDialogEditTask(" + finding.Id + "," + finding.IdFindingTask + ",'" + finding.StateFindingTask + "')\">Edit</a>";
                    aaData.Add(new string[8]{     linkEdit,
                                                  finding.Clause==""?"":finding.Clause.Split('#')[1], 
                                                  finding.Finding, 
                                                  finding.Correction,  
                                                  finding.RootCause, 
                                                  finding.Corrective,                                                  
                                                  finding.LstCompleteDate,
                                                  finding.StateFindingTask
                                                });

                }
                rs.aaData = aaData;
                return rs;
            }
            catch
            {
                return null;
            }
        }

        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        public DataList GetFindingTaskByMe()
        {

            try
            {
                var rs = new DataList
                {
                    aaData = new List<string[]>()
                };
                FunctionFinding functionFinding = new FunctionFinding();
                List<FindingDTO> lst = new List<FindingDTO>();
                lst = functionFinding.GetFindingTaskByMe();
                List<string[]> aaData = new List<string[]>();
                foreach (FindingDTO finding in lst)
                {
                    string linkEdit = "<a href='#' onclick=\"OpenDialogEditTask(" + finding.Id + "," + finding.IdFindingTask + ",'" + finding.StateFindingTask + "')\">Edit</a>";
                    aaData.Add(new string[8]{     linkEdit,
                                                  finding.Clause==""?"":finding.Clause.Split('#')[1], 
                                                  finding.Finding, 
                                                  finding.Correction,  
                                                  finding.RootCause, 
                                                  finding.Corrective,                                                  
                                                  finding.LstCompleteDate,
                                                  finding.StateFindingTask
                                                });

                }
                rs.aaData = aaData;
                return rs;
            }
            catch
            {
                return null;
            }
        }

        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        public DataList GetFindingForMe()
        {

            try
            {
                var rs = new DataList
                {
                    aaData = new List<string[]>()
                };
                FunctionFinding functionFinding = new FunctionFinding();
                List<FindingDTO> lst = new List<FindingDTO>();
                lst = functionFinding.GetFindingForMe();
                List<string[]> aaData = new List<string[]>();
                foreach (FindingDTO finding in lst)
                {
                    string linkEdit = "<a href='#' onclick=\"OpenDialogEditTask(" + finding.Id + "," + finding.IdFindingTask + ",'" + finding.StateFindingTask + "')\">Edit</a>";
                    aaData.Add(new string[15]{     
                                                linkEdit,
                                                   finding.Clause==""?"":finding.Clause.Split('#')[1], 
                                                  finding.AuditeeDepartment==""?"":finding.AuditeeDepartment.Split('#')[1], 
                                                  finding.System==""?"":finding.System.Split('#')[1], 
                                                  finding.Classification==""?"":finding.Classification.Split('#')[1], 
                                                  finding.Auditor==""?"":finding.Auditor.Split('#')[1],
                                                  finding.LeadAuditor==""?"":finding.LeadAuditor.Split('#')[1],                                                  
                                                  finding.Auditee==""?"":finding.Auditee.Split('#')[1],
                                                  finding.PersonInChage==""?"":finding.PersonInChage.Split('#')[1],
                                                  finding.Finding,
                                                  finding.Correction,  
                                                  finding.RootCause, 
                                                  finding.Corrective,                                                  
                                                  finding.LstCompleteDate,
                                                  finding.StateFindingTask
                                                });

                }
                rs.aaData = aaData;
                return rs;
            }
            catch
            {
                return null;
            }
        }
    }
}
